package com.yyw.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yyw.bean.Group;
import com.yyw.bean.User;
import com.yyw.dao.GroupDao;
import com.yyw.util.MyContext;
import com.yyw.util.Result;
import com.yyw.util.ResultFactory;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags = "小组API")
@RestController
@RequestMapping("/user")
@Slf4j
public class GroupController {

    @Autowired
    private GroupDao groupDao;

    @CrossOrigin
    @ApiOperation(value = "获取指定小组信息", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "groupid", value = "小组id") })
    @GetMapping("/getUsersByGroupId")
    public Result getUsersByGroupId(@RequestParam("groupid") Long groupid) {
        if (groupid == null) {
            return ResultFactory.notice("数据不合法");
        }
        Group group = groupDao.getGroupById(groupid);
        return ResultFactory.success(group);
    }

    @CrossOrigin
    @ApiOperation(value = "获取指定小组中的学生信息", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "groupid", value = "小组id") })
    @GetMapping("/getGroupById")
    public Result getGroupById(@RequestParam("groupid") Long groupid) {
        if (groupid == null) {
            return ResultFactory.notice("数据不合法");
        }

        Group group = groupDao.getGroupById(groupid);
        if (group == null) {
            return ResultFactory.notice("小组不存在");
        }
        List<User> list = groupDao.getUsersByGroupId(groupid);
        return ResultFactory.success(list);
    }

    @CrossOrigin
    @ApiOperation(value = "退出小组", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "groupid", value = "小组id") })
    @PostMapping("/exitGroup")
    @Transactional
    public Result exitGroup(@RequestParam("groupid") Long groupid) {
        if (groupid == null) {
            return ResultFactory.notice("数据不合法");
        }
        Group group = groupDao.getGroupById(groupid);
        if (group == null) {
            return ResultFactory.notice("小组不存在");
        }
        User user = MyContext.getSessionUser();
        int i = groupDao.exitGroup(user.getId(), groupid);
        if (i > 0) {
            group.setNumber(group.getNumber() - 1);
            groupDao.updateGroup(group);
            return ResultFactory.success("退出成功");
        } else {
            return ResultFactory.failure("退出失败");
        }
    }

    @CrossOrigin
    @ApiOperation(value = "加入小组", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "groupid", value = "小组id") })
    @PostMapping("/intoGroup")
    @Transactional
    public Result intoGroup(@RequestParam("groupid") Long groupid) {
        if (groupid == null) {
            return ResultFactory.notice("数据不合法");
        }
        Group group = groupDao.getGroupById(groupid);
        if (group == null) {
            return ResultFactory.notice("小组不存在");
        }
        User user = MyContext.getSessionUser();
        int i = groupDao.intoGroup(user.getId(), groupid);
        if (i > 0) {
            group.setNumber(group.getNumber() + 1);
            groupDao.updateGroup(group);
            return ResultFactory.success("成功加入");
        } else {
            return ResultFactory.failure("加入失败");
        }
    }

    @CrossOrigin
    @ApiOperation(value = "创建小组", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "name", value = "小组名字", required = true),
            @ApiImplicitParam(name = "intro", value = "简介", required = true) })
    @PostMapping("/createGroup")
    @Transactional
    public Result createGroup(@RequestParam("name") String name, @RequestParam("intro") String intro) {
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(intro)) {
            return ResultFactory.notice("数据不合法");
        }
        User user = MyContext.getSessionUser();
        Long classid = user.getClassid();
        if (classid == null) {
            return ResultFactory.notice("未加入班级");
        }

        Group group = new Group(null, name, 0, intro, classid, user.getId(), null);
        int i = groupDao.saveGroup(group);
        if (i > 0) {
            return intoGroup(group.getId());
        } else {
            return ResultFactory.failure("创建失败");
        }
    }

    @CrossOrigin
    @ApiOperation(value = "获取用户加入的小组信息", httpMethod = "GET")
    @GetMapping("/getClassGroups")
    public Result getClassGroups() {
        User user = MyContext.getSessionUser();
        Long classid = user.getClassid();
        if (classid == null) {
            return ResultFactory.notice("未加入班级");
        }

        List<Group> list = groupDao.getGroupsByUserId(user.getId(), null);
        return ResultFactory.success(list);
    }
}
